<script setup lang="ts">
import { routes } from '@/router/index'
import tabbar from '@/components/tabbar/tabbar.vue'
import { computed } from '@vue/reactivity';
import { useRoute } from 'vue-router'
import { store } from '@/stores/allStores'
import { onMounted } from 'vue';
import { getStoreTime, isEmpty, isNotEmpty, UUIDGeneratorBrowser } from '@/util/localTool'
import { detailSystemAppApi } from '@/api/system'
import { userStore } from '@/stores/userStore/userStore'
import { appStore } from '@/stores/appStore/appStore'
import { tokenOauth } from './api/auth';
const useroute = useRoute()
//components映射路由
const components = computed(() => {
  let curPage = routes.find(item => {
    return useroute.name === item.name
  })
  if (curPage) {
    return curPage.component
  } else {
    return routes[1].component //home
  }
})
const user = userStore()
onMounted(() => {
  user.clearToken()
  appStore().appId = '192.168.124.202:8080'
  if(isEmpty(user.deviceId)){
    user.setData('deviceId', UUIDGeneratorBrowser())
  }
  detailSystemAppApi().then((res: any) => {
    if (isNotEmpty(res)) {
      user.setData('sysApp', res.data)
      user.setData('sysAppId', res.data.id)
      user.setData('tenantId', res.data.tenantId)
    }
  })
  if (isNotEmpty(user.token)) {
    let tokenTime = getStoreTime('token')
    console.info('token',tokenTime)
    if (tokenTime) {
      let curTimeDur = new Date().getTime() - tokenTime
      if (curTimeDur > 1000 * 518400) {
        user.cleanUserToken()
        console.info('清除token')
      } else if (curTimeDur > 1000 * 259200) {
        console.info('刷新token')
        let temp = {
          grantType: 'refresh_token',
          refreshToken: user.refreshToken
        }
        tokenOauth(temp).then(res => {
          console.info('shuju',res)
          user.setData('refreshToken', res.data.refreshToken)
          user.setData('token', res.data.token)
        })
      }
    }
  }

})
</script>

<template>
  <div>
    <RouterView #default>
      <KeepAlive :include="store.routerStore.cahceList">
        <components :is="components"></components>
      </KeepAlive>
    </RouterView>
    <tabbar v-if="useroute.meta.tabbar" />
  </div>
</template>
